@echo off
chcp 65001 >nul
echo ========================================
echo Tiny QA Windows 启动脚本
echo ========================================

REM 检查虚拟环境
if not exist env (
    echo ❌ 错误: 虚拟环境不存在
    echo 请先运行: scripts\install.bat
    pause
    exit /b 1
)

REM 激活虚拟环境
echo 🔄 激活虚拟环境...
call env\Scripts\activate.bat
if errorlevel 1 (
    echo ❌ 激活虚拟环境失败
    pause
    exit /b 1
)

REM 检查依赖
echo 📚 检查依赖...
python -c "import flask, chromadb, sentence_transformers" >nul 2>&1
if errorlevel 1 (
    echo ❌ 依赖不完整，请运行: scripts\install.bat
    pause
    exit /b 1
)

REM 检查Ollama
echo 🤖 检查Ollama连接...
ollama --version >nul 2>&1
if errorlevel 1 (
    echo ⚠️ 警告: 未找到Ollama
    echo 请先安装并启动Ollama: https://ollama.ai
    echo.
    echo 是否继续启动Tiny QA? (y/n)
    set /p choice=
    if /i not "%choice%"=="y" (
        echo 启动已取消
        pause
        exit /b 0
    )
) else (
    echo ✅ Ollama已安装
)

REM 创建必要目录
if not exist data\database mkdir data\database
if not exist data\documents mkdir data\documents
if not exist data\vectors mkdir data\vectors
if not exist logs mkdir logs

REM 获取端口配置
for /f "tokens=2 delims=:" %%i in ('python -c "import yaml; print(yaml.safe_load(open('config/settings.yaml', 'r', encoding='utf-8'))['app']['port'])" 2^>nul') do set port=%%i
if not defined port set port=5002

echo.
echo 🚀 启动 Tiny QA 服务...
echo 访问地址: http://localhost:%port%
echo API地址: http://localhost:%port%/api
echo 按 Ctrl+C 停止服务
echo.

REM 启动应用
python app.py

pause 